﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameJamKingdom
{
    class Lottery
    {
        Dictionary<string, int> participants;
        Random rand;
        int totalParts;

        public Lottery(Random cRand)
        {
            participants = new Dictionary<string, int>();
            rand = cRand;
            totalParts = 0;
        }

        public void addMember(string name, int parts)
        {
            participants.Add(name, parts);
            totalParts = totalParts + parts;
        }

        public string pickWinner()
        {
            int tarVal = rand.Next(totalParts);

            int i = 0;
            string winner = "error";
            while (winner.Equals("error") && i < participants.Count)
            {
                if (participants.ElementAt(i).Value > tarVal)
                {
                    winner = participants.ElementAt(i).Key;
                }
                tarVal = tarVal - participants.ElementAt(i).Value;
                i++;
                
            }

            if (winner.Equals("error"))
                winner = "empty";

            return winner;
        }
    }
}
